package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.user.ApplyRecordResult;
import com.zhongxi.guoyuan.utils.CommonTool;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/18.
 */

public class ApplyRecordAdapter extends BaseAdapter {

    private Context mContext;
    private List<ApplyRecordResult.ApplyRecordData> list;


    public ApplyRecordAdapter(Context context, ArrayList<ApplyRecordResult.ApplyRecordData> data) {
        this.mContext = context;
        this.list = data;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_apply_recorder, parent, false);
            holder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
            holder.tvDigitalText = (TextView) convertView.findViewById(R.id.tvDigital_text);
            holder.ivPoint = (ImageView) convertView.findViewById(R.id.ivpoint);
            holder.llBackground= (LinearLayout) convertView.findViewById(R.id.linearBackground);
            holder.tvFailReason= (TextView) convertView.findViewById(R.id.fail_reason);
            holder.tvAppliprice= (TextView) convertView.findViewById(R.id.tvApplyPrice);
            holder.tvNUm= (TextView) convertView.findViewById(R.id.tvNumber);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (null != list.get(position) && !"".equals(list.get(position))) {
            ApplyRecordResult.ApplyRecordData data = list.get(position);
            if (null != data.getCtime() && !"".equals(data.getCtime())) {
                holder.tvDate.setText(CommonTool.formatDate(data.getCtime()));
            } else {
                holder.tvDate.setText("");
            }

            if (null != data.getAmount() && !"".equals(data.getAmount())) {
                holder.tvAppliprice.setText(data.getAmount());
            } else {
                holder.tvAppliprice.setText("");
            }
            if(null!=data.getId()&&!"".equals(data.getId())){
                holder.tvNUm.setText(data.getId());
            }else{
                holder.tvNUm.setText("");
            }
            if(null!=data.getPaymount()&&!"".equals(data.getPaymount())){
                holder.tvPrice.setText(data.getPaymount());
            }else{
                holder.tvPrice.setText("");
            }
            if (null!=data.getStatus()&&!"".equals(data.getStatus())){
                if (data.getStatus().equals("0")){
                    holder.tvDigitalText.setText("受理中");
                    holder.tvDigitalText.setTextColor(mContext.getResources().getColor(R.color.bg_yield));
                    holder.ivPoint.setVisibility(View.VISIBLE);

                    holder.llBackground.setBackgroundColor(mContext.getResources().getColor(R.color.background));
                    holder.tvFailReason.setVisibility(View.GONE);

                }else if (data.getStatus().equals("1")){
                    holder.tvDigitalText.setText("已发放");
                    holder.tvDigitalText.setTextColor(mContext.getResources().getColor(R.color.title_red));
                    holder.ivPoint.setVisibility(View.GONE);

                    holder.llBackground.setBackgroundColor(mContext.getResources().getColor(R.color.background));
                    holder.tvFailReason.setVisibility(View.GONE);
                }else {
                    holder.ivPoint.setVisibility(View.GONE);
                    holder.tvDigitalText.setText("已失败");
                    holder.tvDigitalText.setTextColor(mContext.getResources().getColor(R.color.bg_yield));

                    holder.llBackground.setBackgroundColor(mContext.getResources().getColor(R.color.line_color_two));
                    holder.tvFailReason.setVisibility(View.VISIBLE);
                    if(null!=data.getErrormsg()&&!"".equals(data.getErrormsg())){
                        if (data.getErrorcode() != null) {
                            if (!data.getErrorcode().equals("NOTENOUGH"))
                                holder.tvFailReason.setText("失败原因："+data.getErrormsg());
                            else
                                holder.tvFailReason.setText("失败原因：系统正在补充金额");
                        }

                    }else{
                        holder.tvFailReason.setVisibility(View.GONE);
                    }

                }
            }

        }

        return convertView;
    }

    class ViewHolder {
        private TextView tvDate;
        private TextView tvPrice;
        private TextView tvDigitalText;
        private ImageView ivPoint;
        private LinearLayout llBackground;
        private TextView tvFailReason;
        private TextView tvNUm;
        private TextView tvAppliprice;

    }
}
